﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;

public partial class Mycontrol_gio_hang : System.Web.UI.UserControl
{
    protected static int tong_tien;
    protected void Page_Load(object sender, EventArgs e)
    {
        
        rpt_gio_hang.ItemDataBound += new RepeaterItemEventHandler(rpt_gio_hang_ItemDataBound);
        rpt_gio_hang.ItemCommand += new RepeaterCommandEventHandler(rpt_gio_hang_ItemCommand);
        if (!IsPostBack)
        {
            
            layGioHang();
        }
    }

    void layGioHang()
    {
        tong_tien = 0;
        List<ObjGioHang> lstGioHang = new List<ObjGioHang>();
        if (Session["gio_hang"] != null)
        {
            lstGioHang = (List<ObjGioHang>)Session["gio_hang"];
            rpt_gio_hang.DataSource = lstGioHang;
            rpt_gio_hang.DataBind();          

        }
        else
        {
            lbltongtien.Text = "Chưa có tour";
        }
    }

    void rpt_gio_hang_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string strCA = e.CommandArgument.ToString();
        List<ObjGioHang> lst = (List<ObjGioHang>)Session["gio_hang"];
        switch (e.CommandName)
        {
            case "Update":
                foreach (ObjGioHang item in lst)
                {
                    if (item.Tour_id == int.Parse(strCA))
                    {
                        TextBox txt = (TextBox)e.Item.FindControl("txt_so_luong");
                        item.So_luong = int.Parse(txt.Text);
                        Session["gio_hang"] = lst;
                        Response.Redirect(HttpContext.Current.Request.Url.ToString(), true);
                    }
                }
                break;

            case "Delete":
                foreach (ObjGioHang item in lst)
                {
                    if (item.Tour_id == int.Parse(strCA))
                    {
                        lst.Remove(item);
                        Response.Redirect(HttpContext.Current.Request.Url.ToString(), true);
                    }
                }
                break;

        }
    }

    void rpt_gio_hang_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        int gia =int.Parse( DataBinder.Eval(e.Item.DataItem, "gia").ToString());
        int so_luong = int.Parse(DataBinder.Eval(e.Item.DataItem, "so_luong").ToString());
        tong_tien += gia * so_luong;
        lbltongtien.Text = String.Format("{0:0,0}", tong_tien) + Constants.LOAI_TIEN ;
    }

    protected void btnmuatiep_Click(object sender, EventArgs e)
    {

        Response.Redirect("/");
    }
    protected void btndathang_Click(object sender, EventArgs e)
    {
        sp_Hied.Visible = true;
        sp_Show.Visible = false;
        
    }


    protected void btn_dat_hang_Click(object sender, EventArgs e)
    {
        //check so lieu
        Utils utils=new Utils();
        List<WebControl> lstWebcontrol = new List<WebControl>();
        lstWebcontrol.Add(txthoten);
        lstWebcontrol.Add(txttinh);
        lstWebcontrol.Add(txtdiachi);
        lstWebcontrol.Add(txtsodienthoai);
        lstWebcontrol.Add(txtemail);
        
        
        
        lb_messenger1.Text=utils.checkNull(lstWebcontrol);
        if (lb_messenger1.Text == "")
        {

            //check so lieu: email, integer

            lb_messenger1.Text = utils.checkDataType(lstWebcontrol);
            if (lb_messenger1.Text == "")
            {
                string body;
                string xml;
                string thong_tin_khach_hang;
                List<ObjGioHang> lstGioHang = new List<ObjGioHang>();
                lstGioHang = (List<ObjGioHang>)Session["gio_hang"];

                //lay thong tin gio hang dua vao xml
                xml="N'<Order Ho_ten=\""+txthoten.Text+"\" Tinh=\""+txttinh.Text+"\" Dia_chi=\""+txtdiachi.Text+"\" So_dien_thoai=\""+txtsodienthoai.Text+"\" Email=\""+txtemail.Text+"\">";

                if (lstGioHang != null)
                {
                    //lay thong tin gio hang
                    //body la chuoi ~ table hien thi trong mail
                    body = "<table width=\"500px\" style=\"margin:0px; padding:0px; border:0px; border-collapse: collapse; \" >  <tr style=\"\">    <td colspan=\"4\" width=\"100%\" align=\"center\" style=\"color:#FF0000; height:50px;\"><h1 style=\"font-size:30px\">Thông tin đặt tour</h1></td>  </tr>  <tr style=\"background-color:#B9C9FE;\">    <td  >Tên tour</td>    <td align=\"center\">Giá</td>    <td align=\"center\">Số chỗ</td>    <td align=\"center\">Thành tiền</td>  </tr>";
                    thong_tin_khach_hang = "<table width=\"500px\" style=\"margin:0px; padding:0px; border:0px; border-collapse: collapse; \" >  <tr style=\"\">    <td colspan=\"4\" width=\"100%\" align=\"center\" style=\"color:#FF0000; height:50px;\"><h1 style=\"font-size:30px\">Thông tin khách hàng</h1></td>  </tr>  <tr style=\"background-color:#B9C9FE;\">    <td  >Họ tên</td>    <td align=\"center\">Tỉnh</td>    <td align=\"center\">Địa chỉ</td>    <td align=\"center\">Điện thoại</td> <td align=\"center\">Email</td> </tr>";
                    thong_tin_khach_hang += "<tr><td>" + txthoten.Text + "</td><td>" + txttinh.Text + "</td><td>" + txtdiachi.Text + "</td><td>" + txtsodienthoai.Text + "</td><td>" + txtemail.Text + "</td></tr>";
                    thong_tin_khach_hang += "</table>";
                    foreach (ObjGioHang item in lstGioHang)
                    {
                        xml+="<ContactOrder San_pham_id=\""+item.Tour_id+"\" So_luong=\""+item.So_luong+"\" Gia=\""+item.Gia+"\"></ContactOrder>";
                        body += "<tr>    <td>" + item.Ten_tour + "</td>    <td align=\"center\">" + item.Gia + Constants.LOAI_TIEN + "</td>    <td align=\"center\">" + item.So_luong + "</td>    <td align=\"center\">" + item.So_luong * item.Gia + Constants.LOAI_TIEN + "</td>  </tr>";
                    }
                    xml += "</Order>'";
                    body += "<tr style=\"background-color:#B9C9FE;\">  <td >&nbsp;</td>    <td>&nbsp;</td>  	<td  align=\"center\">Tổng tiền</td>	  	<td align=\"center\" >" + tong_tien + Constants.LOAI_TIEN + "</td>  </tr></table>";
                    body = thong_tin_khach_hang + body;
                    //gui mail
                    #region[Sendmail]
                    System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage();

                    string mailto =txtemail.Text;
                    string host = "";
                    int post = 0;
                    post = 587;
                    host = "smtp.gmail.com";
                    string cc = "websitedulich2013@gmail.com";
                    mailMessage.From = (new MailAddress(mailto, "Đặt tour Online", System.Text.Encoding.UTF8));
                    mailMessage.To.Add(txtemail.Text);
                    mailMessage.Bcc.Add(cc);
                    mailMessage.Subject = "Thông tin đặt tour online của bạn :";
                    mailMessage.SubjectEncoding = System.Text.Encoding.UTF8;
                    mailMessage.Body = body;

                    mailMessage.IsBodyHtml = true;
                    mailMessage.BodyEncoding = System.Text.Encoding.UTF8;

                    System.Net.NetworkCredential mailAuthentication = new System.Net.NetworkCredential();
                    mailAuthentication.UserName = "websitedulich2013@gmail.com";
                    mailAuthentication.Password = "huy100790";
                    System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587);
                    mailClient.EnableSsl = true;
                    mailClient.UseDefaultCredentials = false;
                    mailClient.Credentials = mailAuthentication;
                    try
                    {
                        mailClient.Send(mailMessage);
                        lb_messenger.Text = "Bạn đã đặt chỗ thành công !!";
                        Session.Clear();
                        rpt_gio_hang.DataSource = null;
                        rpt_gio_hang.DataBind();
                        lbltongtien.Text = "0";
                        sp_Hied.Visible = false;
                        sp_Show.Visible = true;
                        
                        Order order = new Order();
                        order.createOrder(xml);
                    }
                    catch
                    {
                        lb_messenger.Text = "Hệ thống dã nhận đặt hàng của bạn nhưng đã có lỗi xảy ra khi gửi mail cho ban quản trị !!";
                        return;
                    }
                    #endregion

                }
                else
                {
                    lb_messenger1.Text = "Bạn chưa chọn tour";
                }
            }
        }
    }
    protected void btn_tro_lai_Click(object sender, EventArgs e)
    {
        sp_Hied.Visible = false;
        sp_Show.Visible = true;
        
    }
}